﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Threading;
using System.Management;

namespace PulseApp
{
	/// <summary>
	/// Interaction logic for Page2.xaml
	/// </summary>
	public partial class Page2 : Page {

        Person person;

        Device device0;
        //public int currentPosition0 = 0;
        public ArrayList lines0 = new ArrayList();
        public Boolean isMonitoring0 = false;
        public int lastX0 = 0;
        public int lastY0 = 0;
        public int baseLine0 = 500;
        public PointCollection pc0;
        //public Polyline pl0;
        public PolyBezierSegment pbs0;
        public PathSegmentCollection psc0;
        public PathFigure pf0;
        public PathFigureCollection pfc0;
        public PathGeometry pg0;
        public Path path0;
        ArrayList pulseDataArray0;
        ArrayList tempDataArray0;
        PulseWave pulseWave0;

        Device device1;
        //public int currentPosition1 = 0;
        public ArrayList lines1 = new ArrayList();
        public Boolean isMonitoring1 = false;
        public int lastX1 = 0;
        public int lastY1 = 0;
        public int baseLine1 = 600;
        public PointCollection pc1;
        public Polyline pl1;
        public PolyBezierSegment pbs1;
        public PathSegmentCollection psc1;
        public PathFigure pf1;
        public PathFigureCollection pfc1;
        public PathGeometry pg1;
        public Path path1;
        ArrayList pulseDataArray1;
        ArrayList tembDataArray1;
        PulseWave pulseWave1;

        //UserWindow userWindow;
        ArrayList spLines = new ArrayList();// special point array;
        ArrayList tba = new ArrayList();//text block array

        Boolean isMonitoring = false;

        public double currentRate = 3.0;
        public int currentStart;

        public int height;
        public int pulseWaveRange = 2024;


		public Page2() {
            Console.WriteLine("here");
			InitializeComponent();
            if (checkSerialPorts() > 0)
            {
                this.device0 = new Device(0);
                this.pulseDataArray0 = new ArrayList();
                if (checkSerialPorts() > 1)
                {
                    this.device1 = new Device(1);
                    this.pulseDataArray1 = new ArrayList();
                }
            }

            Person aPerson = CommonResources.Instance.getPerson();
            if (aPerson != null)
            {
                this.person = aPerson;
                string userInfo = person.toResult();
                this.userBlock.Text = userInfo;
            }

		}

		private void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            lock (this)
            {
                this.isMonitoring = false;
                this.isMonitoring0 = false;
                this.isMonitoring1 = false;
                if (this.device0.acceptData) this.device0.endMonitoring();
                if (this.device1 != null && this.device1.acceptData) this.device1.endMonitoring();
            }
			NavigationService.Navigate(new Uri("Index.xaml", UriKind.Relative));
		}

        public int checkSerialPorts()
        {
            ManagementClass mc = new ManagementClass("Win32_SerialPort");     //
            ManagementObjectCollection moc = mc.GetInstances();
            int result = 0;
            foreach (ManagementObject mo in moc)
            {
                if (mo.GetPropertyValue("Description").ToString().Trim() == "Silicon Labs CP210x USB to UART Bridge")
                {
                    result++;
                }
            }
            return result;
        }

        public delegate void textDelegate(short aShort);

        public void sync()
        {
            /*lock (this)
            {
                this.lastX0 = 0;
                this.lastX1 = 0;
            }
            Console.WriteLine("Sync Performed");*/
        }


        public void addShort0(short aShort)
        {
            /*if (lines0.Count <= lastX0)
            {
                Line aLine = new Line();
                aLine.Stroke = Brushes.White;
                aLine.StrokeThickness = 2;
                lines0.Add(aLine);
            }*/
            if (lastX0 == 0)
            {
                //Console.WriteLine("start    : " + aShort);
                //lastY0 = aShort;
                //lock (this)
                //{
                    lastX0 = 1;
                    //lastX1 = 1;
                //}
                //Console.WriteLine("Sync Performed");
            }
            else
            {
                //Console.WriteLine("add value: " + aShort);
                Line aLine = (Line)lines0[lastX0];
                aLine.X1 = lastX0;
                aLine.Y1 = baseLine0 - lastY0;
                lastX0=lastX0+6;
                lastY0 = aShort;
                aLine.X2 = lastX0;
                aLine.Y2 = baseLine0 - lastY0;
                lastX0 = lastX0 % 978;
                //this.pulseCanvas.UpdateLayout();

                //if (!aLine.IsLoaded) this.pulseCanvas.Children.Add(aLine);
            }
        }

        public void addShort1(short aShort)
        {
            /*if (lines1.Count <= lastX1)
            {
                Line aLine = new Line();
                aLine.Stroke = Brushes.Blue;
                aLine.StrokeThickness = 2;
                lines1.Add(aLine);
            }*/
            if (lastX1 == 0)
            {
                lastY1 = aShort;
                /*lock (this)
                {
                    lastX1 = 1;
                    lastX0 = 1;
                }
                Console.WriteLine("sync performed");*/
            }
            else
            {
                Line aLine = (Line)lines1[lastX1];
                aLine.X1 = lastX1;
                aLine.Y1 = baseLine1 - lastY1;
                lastX1++;
                lastY1 = aShort;
                aLine.X2 = lastX1;
                aLine.Y2 = baseLine1 - lastY1;
                lastX1 = lastX1 % 978;
                Console.WriteLine(lastX1 - lastX0 + "");

                //if (!aLine.IsLoaded) this.pulseCanvas.Children.Add(aLine);
            }
        }

        public void clearLines()
        {
            if (this.lines0.Count > 0)
            {
                foreach (Line aLine in lines0)
                {
                    this.pulseCanvas.Children.Remove(aLine);
                }
            }
            if (this.lines1.Count > 0)
            {
                foreach (Line aLine in lines1)
                {
                    this.pulseCanvas.Children.Remove(aLine);
                }
            }
        }

        public void drawWave(int id)
        {
            if (id == 0)
            {
                this.pbs0 = new PolyBezierSegment();
                this.pc0 = new PointCollection();
                this.psc0 = new PathSegmentCollection();
                this.pf0 = new PathFigure();
                this.pfc0 = new PathFigureCollection();
                this.pg0 = new PathGeometry();
                this.path0 = new Path();
                int count = 0;
                for (int i = pulseDataArray0.Count - (int)pulseCanvas.Width; i < pulseDataArray0.Count; i++)
                {
                    Point aPoint = new Point(count, baseLine0 - (short)pulseDataArray0[i]);
                    pc0.Add(aPoint);
                    count++;
                }
                pbs0.Points = pc0;
                Console.WriteLine(pc0.Count);
                this.psc0.Add(pbs0);
                pf0.Segments = psc0;
                pfc0.Add(pf0);
                pg0.Figures = pfc0;
                path0.Data = pg0;
                path0.Stroke = Brushes.White;
                path0.StrokeThickness = 2;
                this.pulseCanvas.Children.Add(path0);
                this.pulseCanvas.UpdateLayout();
            }
            else if (id == 1)
            {
                this.pbs1 = new PolyBezierSegment();
                this.pc1 = new PointCollection();
                this.psc1 = new PathSegmentCollection();
                this.pf1 = new PathFigure();
                this.pfc1 = new PathFigureCollection();
                this.pg1 = new PathGeometry();
                this.path1 = new Path();
                int count = 0;
                for (int i = pulseDataArray1.Count - (int)pulseCanvas.Width; i < pulseDataArray1.Count; i++)
                {
                    Point aPoint = new Point(count, baseLine1 - (short)pulseDataArray1[i]);
                    pc1.Add(aPoint);
                    count++;
                }
                pbs1.Points = pc1;
                Console.WriteLine(pc1.Count);
                this.psc1.Add(pbs1);
                pf1.Segments = psc1;
                pfc1.Add(pf1);
                pg1.Figures = pfc1;
                path1.Data = pg1;
                path1.Stroke = Brushes.Blue;
                path1.StrokeThickness = 1;
                this.pulseCanvas.Children.Add(path1);
                this.pulseCanvas.UpdateLayout();
            }
        }

        public void clearSPLines()
        {
            if (this.spLines.Count > 0)
            {
                foreach (Line line in spLines)
                {
                    this.pulseCanvas.Children.Remove(line);
                }
            }
        }

        public void clearTextBlocks()
        {
            if (this.tba.Count > 0)
            {
                foreach (TextBlock tb in tba)
                {
                    this.pulseCanvas.Children.Remove(tb);
                }
            }
        }

        public Boolean earlyWaveOnTop()
        {
            Boolean result = true;
            if (this.pulseWave0 != null && this.pulseWave1 != null)
            {
                for (int i = 2; i < pulseWave0.getSpecialPoints().Count; i++)
                {
                    SpecialPoint spi = ((SpecialPoint)pulseWave0.getSpecialPoints()[i]);
                    if (spi.getType() == SpecialPoint.TYPE_BOTTOM)
                    {
                        int earlyBottom = 0;
                        int laterBottom = 0;
                        for (int j = 0; j < pulseWave1.getSpecialPoints().Count; j++)
                        {
                            SpecialPoint spj = ((SpecialPoint)pulseWave1.getSpecialPoints()[j]);
                            if (spj.getType() == SpecialPoint.TYPE_BOTTOM && spj.getPosition() < spi.getPosition())
                            {
                                earlyBottom = j;
                            }
                            else if (spj.getType() == SpecialPoint.TYPE_BOTTOM && spj.getPosition() > spi.getPosition())
                            {
                                laterBottom = j;
                                break;
                            }
                        }
                        if (((SpecialPoint)pulseWave1.getSpecialPoints()[laterBottom]).getPosition() - ((SpecialPoint)pulseWave0.getSpecialPoints()[i]).getPosition() >
                            ((SpecialPoint)pulseWave0.getSpecialPoints()[i]).getPosition() - ((SpecialPoint)pulseWave1.getSpecialPoints()[earlyBottom]).getPosition())
                        {
                            result = false;
                        }
                        break;
                    }
                }
            }
            return result;
        }


        public void drawWaveSegment(int id, int start, double rate)
        {

            //check the zoom rate
            short heighest = 0;
            short lowest = 255;
            double margins = 0.1;
            for (int i = pulseDataArray0.Count - (int)pulseCanvas.Width; i < pulseDataArray0.Count; i++)
            {
                if ((short)pulseDataArray0[i] > heighest) heighest = (short)pulseDataArray0[i];
                if ((short)pulseDataArray0[i] < lowest) lowest = (short)pulseDataArray0[i];
                if (pulseDataArray1 != null && pulseDataArray1.Count > i && (short)pulseDataArray1[i] > heighest) heighest = (short)pulseDataArray1[i];
                if (pulseDataArray1 != null && pulseDataArray1.Count > i && (short)pulseDataArray1[i] < lowest) lowest = (short)pulseDataArray1[i];
            }
            if (rate > (1 - margins) * this.pulseCanvas.Height / ((double)heighest - (double)lowest))
            {
                rate = (1 - margins) * this.pulseCanvas.Height / ((double)heighest - (double)lowest);
                currentRate = rate;
            }
            //check the end point
            int end = start + (int)(pulseCanvas.Width / rate);
            if (end >= this.pulseDataArray0.Count) end = pulseDataArray0.Count - 1;
            if (pulseDataArray1 != null && end >= this.pulseDataArray1.Count) end = pulseDataArray1.Count - 1;

            Boolean earlyontop = this.earlyWaveOnTop();

            //get arm length
            double armLength = 70;
            /*if (this.userWindow != null && userWindow.user != null && userWindow.user.getArmLength() > 0)
            {
                armLength = userWindow.user.getArmLength();
            }*/
            armLength = armLength / 100; //convert to meter


            if (id == 0)
            {
                this.pbs0 = new PolyBezierSegment();
                this.pc0 = new PointCollection();
                this.psc0 = new PathSegmentCollection();
                this.pf0 = new PathFigure();
                this.pfc0 = new PathFigureCollection();
                this.pg0 = new PathGeometry();
                this.path0 = new Path();
                int count = 0;
                for (int i = start; i < end; i++)
                {
                    Point aPoint = new Point(count * rate, baseLine1 - 30 - ((short)pulseDataArray0[i] - (double)lowest) * rate);
                    pc0.Add(aPoint);
                    count++;
                }
                pbs0.Points = pc0;
                Console.WriteLine(pc0.Count);
                this.psc0.Add(pbs0);
                pf0.Segments = psc0;
                pfc0.Add(pf0);
                pg0.Figures = pfc0;
                path0.Data = pg0;
                path0.Stroke = Brushes.White;
                path0.StrokeThickness = 2;
                if (currentRate > 2) path0.StrokeThickness = 4;
                this.pulseCanvas.Children.Add(path0);
                this.pulseCanvas.UpdateLayout();



                //draw bottom lines
                clearSPLines();
                clearTextBlocks();
                foreach (SpecialPoint sp in this.pulseWave0.getSpecialPoints())
                {
                    if (sp.getType() == SpecialPoint.TYPE_BOTTOM && sp.getPosition() > start && sp.getPosition() < end)
                    {
                        double xValue = (sp.getPosition() - start) * rate;
                        Line aLine = new Line();
                        aLine.X1 = xValue;
                        aLine.X2 = xValue;
                        aLine.Y1 = 0;
                        aLine.Y2 = baseLine1;
                        aLine.Stroke = Brushes.Red;
                        spLines.Add(aLine);

                        //set text
                        TextBlock tb = new TextBlock();
                        tb.FontSize = 14;
                        tb.Text = sp.getPosition() + "";
                        if (earlyontop && pulseDataArray1 != null)
                        {
                            SpecialPoint sp1 = new SpecialPoint();
                            for (int j = 0; j < pulseWave1.getSpecialPoints().Count; j++)
                            {
                                SpecialPoint spj = ((SpecialPoint)pulseWave1.getSpecialPoints()[j]);
                                if (spj.getType() == SpecialPoint.TYPE_BOTTOM && spj.getPosition() > sp.getPosition())
                                {
                                    sp1 = spj;
                                    break;
                                }
                            }
                            tb.Text = sp.getPosition() + "\r\nArm length is " + armLength +
                                "M\r\nPWV is " + armLength * 200 / (sp1.getPosition() - sp.getPosition());
                        }
                        Canvas.SetTop(tb, baseLine1 - 30 - (short)pulseDataArray0[sp.getPosition()]);
                        Canvas.SetLeft(tb, xValue + 5);
                        this.tba.Add(tb);

                        //add elements
                        this.pulseCanvas.Children.Add(tb);
                        this.pulseCanvas.Children.Add(aLine);
                    }
                }
            }
            else if (id == 1 && pulseDataArray1 != null)
            {
                this.pbs1 = new PolyBezierSegment();
                this.pc1 = new PointCollection();
                this.psc1 = new PathSegmentCollection();
                this.pf1 = new PathFigure();
                this.pfc1 = new PathFigureCollection();
                this.pg1 = new PathGeometry();
                this.path1 = new Path();
                int count = 0;
                for (int i = start; i < end; i++)
                {
                    Point aPoint = new Point(count * rate, baseLine1 - ((short)pulseDataArray1[i] - (double)lowest) * rate);
                    pc1.Add(aPoint);
                    count++;
                }
                pbs1.Points = pc1;
                Console.WriteLine(pc1.Count);
                this.psc1.Add(pbs1);
                pf1.Segments = psc1;
                pfc1.Add(pf1);
                pg1.Figures = pfc1;
                path1.Data = pg1;
                path1.Stroke = Brushes.Blue;
                path1.StrokeThickness = 2;
                if (currentRate > 2) path1.StrokeThickness = 4;
                this.pulseCanvas.Children.Add(path1);
                this.pulseCanvas.UpdateLayout();

                foreach (SpecialPoint sp in this.pulseWave1.getSpecialPoints())
                {
                    if (sp.getType() == SpecialPoint.TYPE_BOTTOM && sp.getPosition() > start && sp.getPosition() < end)
                    {
                        double xValue = (sp.getPosition() - start) * rate;
                        Line aLine = new Line();
                        aLine.X1 = xValue;
                        aLine.X2 = xValue;
                        aLine.Y1 = 0;
                        aLine.Y2 = baseLine1;
                        aLine.Stroke = Brushes.Red;
                        spLines.Add(aLine);
                        TextBlock tb = new TextBlock();
                        tb.FontSize = 14;
                        tb.Text = sp.getPosition() + "";
                        if (!earlyontop)
                        {
                            SpecialPoint sp0 = new SpecialPoint();
                            for (int j = 0; j < pulseWave0.getSpecialPoints().Count; j++)
                            {
                                SpecialPoint spj = ((SpecialPoint)pulseWave0.getSpecialPoints()[j]);
                                if (spj.getType() == SpecialPoint.TYPE_BOTTOM && spj.getPosition() > sp.getPosition())
                                {
                                    sp0 = spj;
                                    break;
                                }
                            }
                            tb.Text = sp.getPosition() + "\r\nArm length is " + armLength +
                                "M\r\nPWV is " + armLength * 200 / (sp0.getPosition() - sp.getPosition());
                        }
                        Canvas.SetTop(tb, baseLine1 - (short)pulseDataArray1[sp.getPosition()]);
                        Canvas.SetLeft(tb, xValue + 5);
                        this.tba.Add(tb);
                        this.pulseCanvas.Children.Add(tb);
                        this.pulseCanvas.Children.Add(aLine);
                    }
                }
            }
        }

        public void drawWaves()
        {
            clearLines();
            if (this.pulseDataArray0.Count > pulseCanvas.Width)
            {
                drawWave(0);
            }
            if (this.pulseDataArray1 != null && this.pulseDataArray1.Count > pulseCanvas.Width)
            {
                drawWave(1);
            }
        }


        public void monitoring0()
        {

            Device aDevice = device0;
            this.isMonitoring0 = true;
            aDevice.startMonitoring();
            int count = 0;
            while (isMonitoring)
            {
                short value = aDevice.getByte();
                pulseDataArray0.Add(value);
              //Console.WriteLine("read value " + value);
                if(count == 0)this.pulseCanvas.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new textDelegate(addShort0), value);
                /**/
                count = (count + 1) % 6;
            }
        }

        public void monitoring1()
        {

            Device aDevice = device1;
            this.isMonitoring1 = true;
            aDevice.startMonitoring();
            while (isMonitoring)
            {
                short value = aDevice.getByte();
                pulseDataArray1.Add(value);
                this.pulseCanvas.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new textDelegate(addShort1), value);
                /**/
            }
        }



        private void startBbutton_Click(object sender, RoutedEventArgs e)
        {
            clearSPLines();
            clearTextBlocks();
            if (device0 != null && !isMonitoring0 && device0.isReady())
            {
                //this.bigWaveButton.Visibility = Visibility.Hidden;
                //this.ampButton.Visibility = Visibility.Hidden;
                //this.deAmpButton.Visibility = Visibility.Hidden;
                //this.backwardButton.Visibility = Visibility.Hidden;
                //this.forwardButton.Visibility = Visibility.Hidden;
                this.ampButton0.Visibility = Visibility.Visible;
                //this.ampButton1.Visibility = Visibility.Visible;
                this.deAmpButton0.Visibility = Visibility.Visible;
                //this.deAmpButton1.Visibility = Visibility.Visible;
                if (this.path0 != null && this.path0.IsLoaded)
                {
                    this.pulseCanvas.Children.Remove(path0);
                }
                this.pulseDataArray0 = new ArrayList();
                isMonitoring = true;
                isMonitoring0 = true;
                //Console.WriteLine("width: "+pulseCanvas.Width);
                while (this.lines0.Count < this.pulseCanvas.Width)
                {
                    Line aLine = new Line();
                    aLine.Stroke = Brushes.White;
                    aLine.StrokeThickness = 2;
                    this.lines0.Add(aLine);
                    //this.pulseCanvas.Children.Add(aLine);
                }
                foreach (Line aLine in lines0)
                {
                    if (!aLine.IsLoaded)
                    {
                        this.pulseCanvas.Children.Add(aLine);
                    }
                }
                Thread monitorThread = new Thread(new ThreadStart(this.monitoring0));

                monitorThread.Start();
                this.stopButton.Visibility = Visibility.Visible;
                this.stopButtonTranform.Visibility = Visibility.Visible;
                this.startBbutton.Visibility = Visibility.Hidden;
                this.startBbuttonTransform.Visibility = Visibility.Hidden;
            }
            if (!isMonitoring1 && this.device1 != null && device1.isReady())
            {
                Console.WriteLine("wrong!");
                if (this.path1 != null && this.path1.IsLoaded)
                {
                    this.pulseCanvas.Children.Remove(path1);
                }
                this.pulseDataArray1 = new ArrayList();
                isMonitoring = true;
                isMonitoring1 = true;
                while (this.lines1.Count < this.pulseCanvas.Width)
                {
                    Line aLine = new Line();
                    aLine.Stroke = Brushes.Blue;
                    aLine.StrokeThickness = 2;
                    this.lines1.Add(aLine);
                    //this.pulseCanvas.Children.Add(aLine);
                }
                foreach (Line aLine in lines1)
                {
                    if (!aLine.IsLoaded)
                    {
                        this.pulseCanvas.Children.Add(aLine);
                    }
                }
                Thread monitorThread = new Thread(new ThreadStart(this.monitoring1));

                monitorThread.Start();
                this.stopButton.Visibility = Visibility.Visible;
                this.stopButtonTranform.Visibility = Visibility.Visible;
                this.startBbutton.Visibility = Visibility.Hidden;
                this.startBbuttonTransform.Visibility = Visibility.Hidden;
            }
            //sync();
        }

        private void stopButton_Click(object sender, RoutedEventArgs e)
        {
            lock (this)
            {
                this.isMonitoring = false;
                this.isMonitoring0 = false;
                this.isMonitoring1 = false;
                if (this.device0.acceptData) this.device0.endMonitoring();
                if (this.device1 != null && this.device1.acceptData) this.device1.endMonitoring();
            }
            this.ampButton0.Visibility = Visibility.Hidden;
            //this.ampButton1.Visibility = Visibility.Hidden;
            this.deAmpButton0.Visibility = Visibility.Hidden;
            //this.deAmpButton1.Visibility = Visibility.Hidden;
            if (pulseDataArray0.Count > pulseCanvas.Width)
            {

                //this.bigWaveButton.Visibility = Visibility.Visible;
                drawWaves();
                /*if (this.userWindow != null && userWindow.user.getHeight() > 0)
                {
                    this.height = (int)userWindow.user.getHeight();
                }
                else
                {
                    this.height = 180;
                }*/
                int tempStart = this.pulseDataArray0.Count - this.pulseWaveRange;
                if (tempStart < 0) tempStart = 0;
                ArrayList tempArray = new ArrayList();
                for (int i = tempStart; i < pulseDataArray0.Count; i++)
                {
                    //Console.WriteLine(pulseDataArray0[i]);
                    tempArray.Add(pulseDataArray0[i]);
                }
                pulseDataArray0 = tempArray;
                Console.WriteLine("pulse data array size: "+pulseDataArray0.Count);
                height = person.getHeight();
                this.pulseWave0 = new PulseWave(this.pulseDataArray0, height);
                this.pulseWave0.getSIs();
                CommonResources.Instance.setPulseWave(this.pulseWave0);
                NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative));
                /*this.resultBox.Text = pulseWave0.SIsToString();
                this.currentStart = pulseDataArray0.Count - (int)pulseCanvas.Width;
                if (this.pulseDataArray1 != null && pulseDataArray1.Count > pulseCanvas.Width)
                {
                    tempStart = this.pulseDataArray1.Count - this.pulseWaveRange;
                    if (tempStart < 0) tempStart = 0;
                    ArrayList tempArray1 = new ArrayList();
                    for (int i = tempStart; i < pulseDataArray1.Count; i++)
                    {
                        tempArray1.Add(pulseDataArray1[i]);
                    }
                    this.pulseDataArray1 = tempArray1;
                    this.pulseWave1 = new PulseWave(this.pulseDataArray1, height);
                    this.pulseWave1.getSIs();

                }*/
            }
            this.startBbutton.Visibility = Visibility.Visible;
            this.startBbuttonTransform.Visibility = Visibility.Visible;
            this.stopButton.Visibility = Visibility.Hidden;
            this.stopButtonTranform.Visibility = Visibility.Hidden;
        }

        /*private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (this.userWindow == null) userWindow = new UserWindow();
            userWindow.Show();
            userWindow.Topmost = true;
        }*/

        private void ampButton0_Click(object sender, RoutedEventArgs e)
        {
            if (this.device0 != null)
            {
                device0.amplify();
            }
        }

        private void deAmpButton0_Click(object sender, RoutedEventArgs e)
        {
            if (this.device0 != null)
            {
                device0.deAmplify();
            }
        }

        private void ampButton1_Click(object sender, RoutedEventArgs e)
        {
            if (this.device1 != null)
            {
                device1.amplify();
            }
        }

        private void deAmpButton1_Click(object sender, RoutedEventArgs e)
        {
            if (this.device1 != null)
            {
                device1.deAmplify();
            }
        }

        public void drawSegments()
        {
            if (!isMonitoring0 && !isMonitoring1)
            {
                if (this.path0 != null && this.path0.IsLoaded)
                {
                    this.pulseCanvas.Children.Remove(path0);
                }
                if (this.path1 != null && this.path1.IsLoaded)
                {
                    this.pulseCanvas.Children.Remove(path1);
                }
                clearLines();
                if (this.pulseDataArray0.Count > pulseCanvas.Width && currentStart > 0)
                {
                    drawWaveSegment(0, currentStart, currentRate);
                }
                if (pulseDataArray1 != null && this.pulseDataArray1.Count > pulseCanvas.Width && currentStart > 0)
                {
                    drawWaveSegment(1, currentStart, currentRate);
                }
            }
        }

        /*private void bigWaveButton_Click(object sender, RoutedEventArgs e)
        {
            drawSegments();
            this.ampButton.Visibility = Visibility.Visible;
            this.deAmpButton.Visibility = Visibility.Visible;
            this.backwardButton.Visibility = Visibility.Visible;
            this.forwardButton.Visibility = Visibility.Visible;
        }*/

        private void ampButton_Click(object sender, RoutedEventArgs e)
        {
            double addon = 0.1;
            try
            {
                double temp = Double.Parse(this.resultBox.Text);
                if (temp > 0) addon = temp;
            }
            catch (Exception ex) { }
            this.currentRate += addon;
            drawSegments();
        }

        private void deAmpButton_Click(object sender, RoutedEventArgs e)
        {
            double addon = 0.1;
            try
            {
                double temp = Double.Parse(this.resultBox.Text);
                if (this.currentRate - addon > 1) addon = temp;
            }
            catch (Exception ex) { }
            if (this.currentRate - addon > 1) this.currentRate -= addon;
            drawSegments();
        }

        private void backwardButton_Click(object sender, RoutedEventArgs e)
        {
            int addon = 10;
            try
            {
                int temp = Int32.Parse(this.resultBox.Text);
                if (!(this.currentStart + temp > pulseDataArray0.Count) && !(this.currentStart + temp > pulseDataArray1.Count))
                {
                    addon = temp;
                }
            }
            catch (Exception ex)
            {
            }
            if (!(this.currentStart + addon > pulseDataArray0.Count) && (pulseDataArray1 != null && !(this.currentStart + addon > pulseDataArray1.Count)))
            {
                this.currentStart += addon;
            }
            if (!(this.currentStart + addon > pulseDataArray0.Count) && pulseDataArray1 == null)
            {
                this.currentStart += addon;
            }
            drawSegments();
        }

        private void forwardButton_Click(object sender, RoutedEventArgs e)
        {
            int addon = 10;
            try
            {
                int temp = Int32.Parse(this.resultBox.Text);
                if (this.currentStart - temp > 0)
                {
                    addon = temp;
                }
            }
            catch (Exception ex)
            {
            }
            if (this.currentStart - addon > 0)
            {
                this.currentStart -= addon;
            }
            drawSegments();
        }


	}
}
